<html>
    <head>
        <meta charset="UTF-8">

        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js" integrity="sha256-NGC9JEuTWN4GhTj091wctgjzftr+8WNDmw0H8J5YPYE=" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/gh/InventivetalentDev/MineRender@1.4.6/dist/skin.min.js"></script>

    <link rel="stylesheet" href="src/css/change_page.css">

    </head>

    <body>
        <?php
            echo "<header>";
            include_once "for_include/check_user.php";
            echo "你输入的名字是：".$_GET['username']."<br>";
            //echo $_GET['change_code'];

            echo "<br>";

           


            $sql_config=include "server_config/config.php";
            $dbobj = new mysqli($sql_config['db_host'],$sql_config['db_user'],$sql_config['db_pw'],$sql_config['db_name'],$sql_config['db_port']);
            if($dbobj->connect_error)
            {
                die("数据库连接失败: ".$dbobj->connect_error."<br>");
            }
            else
            {
                echo "数据库连接成功 <br>";
            }




            $username=$_GET['username'];

            $dbobj->query("set names utf8");
            


            if(!check_user($username))
            {
                die("账号不存在！请先注册！<br><a href=\"index.php\">点我回到上一页</a>");
            }
            
            $stmt=$dbobj->prepare("select * from mc_skin_con where username = ?");

            
            $stmt->bind_param("s",$username);
            
            $stmt->execute();
            
            $stmt->bind_result($id,$name,$changeCode,$skins_default,$skins_slim,$skins_cape,$skins_elytra);




            if($stmt->fetch())
            {
                echo $name;
            }
            else
            {
                echo "没有记录";
            }


            if($_GET['change_code']!=$changeCode)
            {
                echo "<br>"."你的修改码不匹配，无权更改!";
                echo "</header><main>";
                
            }
            else
            {
                echo "你的修改码匹配，可以更改!   本页面使用了开源项目MineRender。<br>";
                echo "先点击浏览，选中你想要上传的皮肤文件<br>再点击上传，来进行上传操作<br>";
                echo "下面的披风预览显示异常，但是在游戏中是没问题的哈哈。<br>";
                echo "</header><main>";
                
                change_code("这是你的普通皮肤","1");
                change_code("这是你的瘦手臂皮肤","2");
                change_code("这是你的披风皮肤","3");
                change_code("这是你的鞘翅皮肤","4");

                echo"
                    <form >
                
                ";

            }

            echo "</main>";
            
           
            function change_code(string $tell_txt,string $type_id)
            {
                global $username,$changeCode;
                global $skins_de_path;

                $skins_type="";

                global $skins_default,$skins_slim,$skins_cape,$skins_elytra;
                if($type_id=="1") $skins_de_path=$skins_default;
                else if($type_id=="2") $skins_de_path=$skins_slim;
                else if($type_id=="3") $skins_de_path=$skins_cape;
                else if($type_id=="4") $skins_de_path=$skins_elytra;

                echo "
                    
                    <div class=\"show\">
            
                        <div class=\"skins_default\">

                            <div class=\"origin\">
                            
                                {$tell_txt}
                                <img src=\"skin/{$skins_de_path}.png\">
                            </div>

                        </div>
                            <div class=\"skin-container\" id=\"skin{$type_id}\"></div>
                            <div id=\"skin_path{$type_id}_div\" skin_path=\"{$skins_de_path}\"></div>

                            

                            <div class=\"upload\">
                                <form action=\"upload.php\" method=\"post\" enctype=\"multipart/form-data\">
                                    <label for=\"file\">上传新的皮肤:</label>
                                    <input type=\"file\" name=\"file\" id=\"file\" required>

                                    <input type=\"hidden\" name=\"username\" value=\"{$username}\">
                                    <input type=\"hidden\" name=\"old_skin\" value=\"{$skins_de_path}\">
                                    <input type=\"hidden\" name=\"type\" value=\"{$type_id}\">
                                    <input type=\"hidden\" name=\"change_code\" value=\"{$changeCode}\">

                                    <input type=\"submit\" value=\"上传\">
                                </form>
                                <form action=\"delete.php\" method=\"post\" enctype=\"multipart/form-data\">
                                    <input type=\"hidden\" name=\"username\" value=\"{$username}\">
                                    <input type=\"hidden\" name=\"old_skin\" value=\"{$skins_de_path}\">
                                    <input type=\"hidden\" name=\"type\" value=\"{$type_id}\">
                                    <input type=\"hidden\" name=\"change_code\" value=\"{$changeCode}\">
                                    <input type=\"submit\" value=\"删除已有皮肤\">
                                </form>
                            </div>
                        


                    
                    </div>
                
                ";
            }       
        ?>
    </body>
    
    <footer>
    <br>
    <a href="index.php">点我回到上一页</a>
    </footer>



    <script>
        function renderSkin(containerId, skinUrl,capeUrl,slim=false) {
                var container = document.getElementById(containerId);
                var skinRender = new SkinRender({
                    autoResize: true,
                    render: {
                        taa: true,
                        
                    }
                }, container);

                if(capeUrl=="")
                {
                    skinRender.render({
                    url: skinUrl,
                    optifine: true,
                    slim:slim
                    });
                }
                else
                {
                    skinRender.render({
                    url: skinUrl,
                    capeUrl: capeUrl,
                    optifine: true,
                    slim:slim
                    });
                }

                // 默认启用动画
                var animate = true;

                

                // 动画效果
                var startTime = Date.now();
                container.addEventListener("skinRender", function (e) {
                    if (animate) {
                        var t = (Date.now() - startTime) / 1000;
                        e.detail.playerModel.children[2].rotation.x = Math.sin(t * 5) / 2; // 左腿
                        e.detail.playerModel.children[3].rotation.x = -Math.sin(t * 5) / 2; // 右腿
                        e.detail.playerModel.children[4].rotation.x = Math.sin(t * 5) / 2; // 左臂
                        e.detail.playerModel.children[5].rotation.x = -Math.sin(t * 5) / 2; // 右臂
                    }
                });


                var k=0.43;
                container.addEventListener("skinRender", function(e) {
                    // 设置缩放
                    e.detail.playerModel.scale.set(k,k,k); // 将模型缩小为原来的50%
                    
                    // 设置位置
                    e.detail.playerModel.position.set(0, 10.1, 0); // 调整位置以使模型在容器内居中
                });
            }

            var skinsDePaths = {
            1: "<?php echo $skins_default; ?>",
            2: "<?php echo $skins_slim; ?>",
            3: "<?php echo $skins_cape; ?>",
            4: "<?php echo $skins_elytra; ?>"
        };


            // for(var i=1;i<=4;i++)
            // {
            //     var id = "skin_path"+ i+ "_div";
            //     var container = document.getElementById(id);
            //     //var skinUrl = container.getAttribute('data-skin-url');
            //     var skinUrl = "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[i]+".png";
            //     //alert(skinUrl);

                
                
            //     renderSkin("skin"+i, skinUrl,capeUrl);
            // }

            renderSkin("skin1", "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[1]+".png","",false);
            renderSkin("skin2", "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[2]+".png","",true);

            

            if(skinsDePaths[1]!="")
            {
                renderSkin("skin3", "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[1]+".png",
                "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[3]+".png");

                renderSkin("skin4", "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[1]+".png",
                "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[4]+".png");
            }
            
            else if(skinsDePaths[2]!="")
            {
                renderSkin("skin3", "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[2]+".png",
                "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[3]+".png",true);

                renderSkin("skin4", "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[2]+".png",
                "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[4]+".png",true);
            }
            
            else
            {
                renderSkin("skin3", "http://60.205.190.110/mc_skin/skin/0.png",
                "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[3]+".png");

                renderSkin("skin4", "http://60.205.190.110/mc_skin/skin/0.png",
                "http://60.205.190.110/mc_skin/skin/"+skinsDePaths[4]+".png");
            }
            
            



    </script>

</html>
<!-- 
if (location.hash) $("#nameInput").val(location.hash.substring(1));
            $("#nameInput,#capeInput,#slim").on("change", function () {
                skinRender.clearScene();
                var skin = $("#nameInput").val();
                var cape = $("#capeInput").val();
                var slim =  $("#slim").is(":checked");
                var options = {};
                if (skin.indexOf("http") === 0) {
                    options.url = skin;
                } else {
                    options.username = skin;
                    location.hash = skin;
                }
                if (cape && cape.length > 0) {
                    if (cape.includes("capes.dev") || !cape.startsWith("http")) { // capes.dev link or type
                        options.cape = cape;
                    } else {
                        options.capeUrl = cape;
                        options.optifine = cape.toLowerCase().indexOf("optifine") > 0;
                    }
                }
                options.slim = slim;
                skinRender.render(options);
            }) -->